﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using _4T.BLL;
using _4T.Data;
using _4T.Data.IDataContext;
using CMS.Core.Libs;
using Notification;

namespace _4T.ASCX
{
    public partial class ProfileValidateControl : System.Web.UI.UserControl, ICMSConsiderAndDecide
    {

        #region Field

        private List<string> imageArrayPath = new List<string>();
        private List<string> videoArrayPath = new List<string>();
        string profileCode;

        #endregion

        #region Contructor
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["imageArrayPath"] = imageArrayPath;
                Session["videoArrayPath"] = videoArrayPath;
            }
            profileCode = Request.QueryString["pid"];
            if (!string.IsNullOrEmpty(profileCode))
            {
                LoadProfileInfo(profileCode);
            }

            txtTreatTime.Text = DateTime.Now.ToShortDateString();
        }

        private void LoadProfileInfo(string profileInfoCode)
        {
            // Load patient info
            PatientInfoBLL patientBLL = new PatientInfoBLL();
            _4T.Data.PatientInfo patient = patientBLL.GetPatientInfo(profileInfoCode);
            if (patient != null)
            {
                lbPatientName.Text = patient.PatientName;
                lbDOB.Text = patient.Birthday.ToShortDateString();
                ddlGender.SelectedValue = patient.Gender;
                lbIDNumber.Text = patient.IDNumber;
                lbPermAddr.Text = patient.PermanentAddress;
                lbTempAddr.Text = patient.TemporaryAddress;
                lblMobileNumber.Text = patient.MobileNumber;
                lbPhoneNumber.Text = patient.PhoneNumber;
                lbRepresentativeName.Text = patient.EmergencyRepresentative;
                lbRepresentativePhone.Text = patient.RepresentativePhoneNumber;
                lbLocalContactName.Text = patient.LocalContactPoint;
                lbLocalContactPhone.Text = patient.LocalContactPointPhoneNumber;
                lbMedicalStaff.Text = patient.LocalMedicalStaff;
                lbMedicalStaffPhone.Text = patient.LocalMedicalStaffPhoneNumber;
                lbMedicalFacility.Text = patient.MedicalFacility;
                lbNote.Text = patient.Note;
            }

            // Load medical info
            MedicalInfoBLL medicalBLL = new MedicalInfoBLL();
            _4T.Data.MedicalInfo medical = medicalBLL.GetMedicalInfo(profileInfoCode);
            if (medical != null)
            {
                lblDiagnosis.Text = medical.Diagnosis;
                lbMedicalTime.Text = medical.MedicalTime.ToShortDateString();
                lbTreatmentMethod.Text = medical.TreatmentMethod;
                lbSituation.Text = medical.Situation;
                lbSupport.Text = medical.Support;
            }

            // Load info supplier info
            InfoSupplierInfoBLL infoSupplierBLL = new InfoSupplierInfoBLL();
            _4T.Data.InfoSupplierInfo supplier = infoSupplierBLL.GetInfoSupplierInfo(profileInfoCode);
            if (supplier != null)
            {
                lbInfoSupplierName.Text = supplier.SupplierName;
                lbInfoSupplierPhone.Text = supplier.PhoneNumber;
                lbEmail.Text = supplier.Email;
            }
        }
        #endregion

        #region Private

        private void LoadFilePatient(string profileCode)
        {
            /*
            var PatientObj = new PatientInfoBLL().GetPatientInfo(profileCode);
            var filterObj = new ConsiderAndDecideBLL().GetInfoFilterByProfileCode(profileCode).SingleOrDefault();
            if (PatientObj != null)
            {
                lblProfileName.Text = !string.IsNullOrEmpty(PatientObj.PatientName) ? PatientObj.PatientName : string.Empty;
                lblDateOfBirth.Text = PatientObj.Birthday != null ? PatientObj.Birthday.ToString("dd/MM/yyyy") : "";
                lblCMND.Text = !string.IsNullOrEmpty(PatientObj.IDNumber) ? PatientObj.IDNumber : string.Empty;
                lblGender.SelectedValue = PatientObj.Gender;
                lblPhone.Text = !string.IsNullOrEmpty(PatientObj.PhoneNumber) ? PatientObj.PhoneNumber : string.Empty;
                lblAdress1.Text = !string.IsNullOrEmpty(PatientObj.PermanentAddress) ? PatientObj.PermanentAddress : string.Empty;
                lblAdress2.Text = !string.IsNullOrEmpty(PatientObj.TemporaryAddress) ? PatientObj.TemporaryAddress : string.Empty;
                lblProfileCode.Text = profileCode;
            }
            if (filterObj != null)
            {
                lblFilterName.Text = !string.IsNullOrEmpty(filterObj.Filter) ? filterObj.Filter : string.Empty;
                lblFiltDate.Text = filterObj.FilterTime.ToString();
                lblCommentOfFilter.Text = !string.IsNullOrEmpty(filterObj.FilterContent) ? filterObj.FilterContent : string.Empty;
            }*/
        }

        private string GetProfileInfoCodeFromProfileCode(string profileCode)
        {
            return new ProfileInfoBLL().GetProfileInfoCodeFromProfileCode(profileCode);

        }

        private PersonalInfo GetPersonalInfo(string personalCode)
        {
            return new PersonalInfoBLL().GetPersonalInfo(personalCode);
        }
        #endregion


        #region ICMSConsiderAndDecide
        public Guid ConsiderAndDecideID
        {
            get { return Guid.NewGuid(); }
            set { ConsiderAndDecideID = value; }
        }

        public string ConsiderAndDecideName
        {
            get
            {
                if (Session["User"] != null)
                {
                    PersonalInfo currentUser = GetPersonalInfo(Session["User"].ToString());
                    return currentUser != null ? currentUser.FullName : string.Empty;
                }
                return string.Empty;
            }
            set { ConsiderAndDecideName = value; }
        }

        public string DiseaseName
        {
            get { return !string.IsNullOrEmpty(txtDiseaseName.Text) ? txtDiseaseName.Text : string.Empty; }
            set { DiseaseName = value; }
        }

        public string PhaseOfDisease
        {
            get { return !string.IsNullOrEmpty(txtPhaseOfDisease.Text) ? txtPhaseOfDisease.Text : string.Empty; }
            set { DiseaseName = value; }
        }

        public bool IsTreat
        {
            get { return rdoIsTreatYes.Checked ? true : false; }
            set { IsTreat = value; }
        }

        public bool IsRecover
        {
            get { return rdoIsRecoverYes.Checked ? true : false; }
            set { IsRecover = value; }
        }

        public string AnticipateSpend
        {
            get { return !string.IsNullOrEmpty(txtAnticipateSpend.Text) ? txtAnticipateSpend.Text : string.Empty; }
            set { AnticipateSpend = value; }
        }

        public string TreatTime
        {
            get { return !string.IsNullOrEmpty(txtTreatTime.Text) ? txtTreatTime.Text : string.Empty; }
            set { TreatTime = value; }
        }

        public string CommentOfDoctor
        {
            get { return !string.IsNullOrEmpty(txtCommentOfDoctor.Text) ? txtCommentOfDoctor.Text : string.Empty; }
            set { CommentOfDoctor = value; }
        }

        public bool IsPoor
        {
            get { return rdoIsPoorYes.Checked ? true : false; }
            set { IsPoor = value; }
        }

        public bool IsDirectLabour
        {
            get { return rdoIsDirectLabourYes.Checked ? true : false; }
            set { IsDirectLabour = value; }
        }

        public string CauseIll
        {
            get { return !string.IsNullOrEmpty(txtCauseIll.Text) ? txtCauseIll.Text : string.Empty; }
            set { CauseIll = value; }
        }

        private string _imageUrl;
        public string ImageUrl
        {
            get
            {
                imageArrayPath = (Session["imageArrayPath"] as List<string>);
                if (imageArrayPath != null && imageArrayPath.Count > 0)
                {
                    foreach (var item in imageArrayPath)
                    {
                        _imageUrl += item;
                        break;
                    }
                    return _imageUrl;
                }
                else
                {
                    return string.Empty;
                }
            }
            set { _imageUrl = value; }
        }

        private string _videoUrl;
        public string VideoUrl
        {
            get
            {
                videoArrayPath = (Session["videoArrayPath"] as List<string>);
                if (videoArrayPath != null && videoArrayPath.Count != 0)
                {
                    foreach (var item in videoArrayPath)
                    {
                        _videoUrl += item;
                        break;
                    }
                    return _videoUrl;
                }
                else
                {
                    return string.Empty;
                }
            }
            set { _videoUrl = value; }
        }

        public string CommentOfConsider
        {
            get { return !string.IsNullOrEmpty(txtComment.Text) ? txtComment.Text : string.Empty; }
            set { CommentOfConsider = value; }
        }

        public string ProfileName
        {
            get { return !string.IsNullOrEmpty(Request.QueryString["pid"]) ? Request.QueryString["pid"].ToString() : string.Empty; }
            set { ProfileName = value; }
        }

        public DateTime CreatedDate
        {
            get { return DateTime.Now; }
            set { CreatedDate = value; }
        }
        #endregion

        #region Event

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string sessionVideoUrl = VideoUrl;
            string sessionImageUrl = ImageUrl;
            Session["imageArrayPath"] = null;
            Session["videoArrayPath"] = null;

            int updateStatus = 0;
            int updateStage = (int)ProfileStage.Approve;
            if (rdoNotCheck.Checked == true)
            {
                updateStatus = 2;
                updateStage = updateStage - 1;
            }
            ProfileStatus profileStatus = new ProfileStatus
            {
                ProfileCode = profileCode,
                Verifier = Session["User"] != null ? Session["User"].ToString() : string.Empty,
                Stage = updateStage,
                Status = updateStatus
            };
            DateTime createdDate = new DateTime();

            try
            {
                if (!string.IsNullOrEmpty(txtCreatedDate.Text))
                    createdDate = Convert.ToDateTime(txtCreatedDate.Text);
            }
            catch
            {
                createdDate = DateTime.Now;
            }
            int resultStageLink = new ProfileStageLinkBLL().InsertProfileStageLink(profileCode, (int)ProfileStage.Verify, sessionVideoUrl, null, sessionImageUrl, this.CommentOfConsider, createdDate);
            if (new ConsiderAndDecideBLL(this).AddConsiderAndDecide() > 0 && new ProfileStatusBLL().UpdateProfileStatus(profileStatus))
            {
                System.Threading.Thread thread = new System.Threading.Thread(() => threadSendNotification(profileCode));
                thread.IsBackground = true;
                thread.Start();
                HttpContext.Current.Response.Redirect("~/ProfileValidateList.aspx");
            }
        }

        private void threadSendNotification(string profileCode)
        {
            DAL.NotificationDAL notificationDAL = new DAL.NotificationDAL();
            notificationDAL.sendNotification(@"Hồ Sơ " + profileCode + " vừa được xác minh, vui lòng phê duyệt", 5);
        }
        #endregion
    }
}